package io.github.wanggit.antrpc.client.config.workers;

import io.github.wanggit.antrpc.IAntrpcContext;
import io.github.wanggit.antrpc.commons.breaker.IResilience4jCircuitBreakerContainer;
import io.github.wanggit.antrpc.commons.config.CircuitBreakerConfig;
import io.github.wanggit.antrpc.commons.config.ConfigChangeTypeEnum;
import io.vavr.collection.List;

public class GlobalBreakerConfigChangeWorker extends AbstractChangeWorker {

    public GlobalBreakerConfigChangeWorker(IAntrpcContext antrpcContext, List<String> prefixs) {
        super(antrpcContext, prefixs);
    }

    @Override
    protected synchronized void internalFired(
            ConfigChangeTypeEnum changeType, String key, String oldValue, String newValue) {
        CircuitBreakerConfig globalBreakerConfig =
                getAntrpcContext().getConfiguration().getGlobalBreakerConfig();
        if (null == globalBreakerConfig) {
            globalBreakerConfig = new CircuitBreakerConfig();
            getAntrpcContext().getConfiguration().setGlobalBreakerConfig(globalBreakerConfig);
        }
        setFieldValue(globalBreakerConfig, key, newValue);
        IResilience4jCircuitBreakerContainer resilience4jCircuitBreakerContainer =
                getAntrpcContext().getResilience4jCircuitBreakerContainer();
        resilience4jCircuitBreakerContainer.reInitGlobalConfig(
                getAntrpcContext().getConfiguration());
    }
}
